package grape.file.service.cloudstorage.impl;

import cn.hutool.core.date.DateUtil;
import grape.common.tools.PathTool;
import grape.common.tools.ToolService;
import grape.file.service.cloudstorage.api.ApiCloudStorageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.UUID;

/**
 * 云存储抽象实现
 * Created by yangwei
 * Created at 2019/3/14 11:21
 */
public abstract class AbstractCloudStorageService implements ApiCloudStorageService, ToolService {


    @Value("${grape.cloudstorage.result-concat-domain}")
    private boolean resultConcatDomain;

    /**
     * 新文件路径
     * @param prefix 前缀
     * @param suffix 后缀 扩展名不带点
     * @return 返回上传路径
     */
    public String newPath(String prefix, String suffix) {
        //文件路径
        String path = DateUtil.formatDate(DateUtil.date()) + PathTool.SLASH + uuid()
                + (isStrEmpty(suffix)? "" : PathTool.ensureBeginDot(suffix));
        if(!isStrEmpty(prefix)){
            path = PathTool.ensureNotEndSlash(prefix) + PathTool.SLASH + path;
        }
        return path;
    }

    public String getResult(String domain,String prefix,String filePath){
        if (!resultConcatDomain) {
            domain = null;
        }
        return PathTool.concat(domain ,prefix , filePath);
    }
}
